package org.jiangpeng.customer.service;

import org.jiangpeng.customer.helper.DatabaseHelper;
import org.jiangpeng.customer.model.Customer;
import org.jiangpeng.customer.util.PropsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 客户管理服务service
 *
 * @author jiangpeng
 */
public class CustomerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);

    private static final String SELECT_CUSTOMER_INFO = "SELECT customer.id,customer.`name`,customer.contact,customer.telephone,customer.email,customer.remake FROM customer";

    /**
     * 获取说有客户信息
     * @return
     */
    public List<Customer> getCustomerList(){
        try {
            String sql = SELECT_CUSTOMER_INFO;
            return DatabaseHelper.queryEntityList(Customer.class, sql);
        }catch (Exception e){
            LOGGER.error("query Customer list failure", e);
        }
        return null;
    }

    /**
     * 根据id获取客户信息
     * @param id
     * @return
     */
    public  Customer getCustomer(long id){
        try {
            String sql = SELECT_CUSTOMER_INFO + " WHERE ID = ?";
            return DatabaseHelper.queryEntity(Customer.class, sql, id);
        } catch (Exception e) {
            LOGGER.error("query entity failure", e);
        }
        return null;
    }

    /**
     * 创建客户
     * @return
     */
    public boolean createCustomer(Map<String, Object> fieldMap){
        try {
            return DatabaseHelper.insertEntity(Customer.class, fieldMap);
        } catch (Exception e) {
            LOGGER.error("insert Customer failure", e);
        }
        return  false;
    }

    /**
     * 修改客户信息
     * @param fieldMap
     * @param id
     * @return
     */
    public  boolean updateCustomer(Map<String, Object> fieldMap, long id){
        try {
            return DatabaseHelper.updateEntity(Customer.class, id, fieldMap);
        } catch (Exception e) {
            LOGGER.error("update Customer failure", e);
        }
        return  false;
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    public  boolean deleteCustomer(long id){
        try {
            return DatabaseHelper.deleteEntity(Customer.class, id);
        } catch (Exception e) {
            LOGGER.error("delete Customer failure", e);
        }
        return false;
    }
}
